/**
 * Copyright 2010 Creative Works, Inc.
 * Creative Works licenses this file to you under the Apache License, version
 * 2.0 (the "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *    http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.  See the License for the specific language governing
 * permissions and limitations under the License.
 */
package org.sendvox.freswitch.core.impl;

import java.util.ArrayList;
import java.util.List;

import org.sendvox.freswitch.core.ApiCommand;
import org.sendvox.freswitch.core.Command;
import org.sendvox.freswitch.core.CommandException;
import org.sendvox.freswitch.inbound.InboundClient;
import org.sendvox.freswitch.outbound.OutboundClient;
import org.sendvox.freswitch.util.Arguments;

/**
 * Send an api command (blocking mode)
 * <p>
 * <source>&nbsp; api &lt;command&gt; &lt;arg&gt; </source>
 * </p>
 * Api commands are documented in the mod_commands section. Examples:
 * <p>
 * <source>&nbsp; api originate sofia/mydomain.com/ext@yourvsp.com 1000
 * </source>
 * </p>
 * This command connects <source>sip:ext@yourvsp.com</source> to extension
 * <source>1000</source>
 * <p>
 * <source>&nbsp; api sleep 5000</source>
 * </p>
 * <p>
 * <b>Reference: </b><a
 * href="http://wiki.freeswitch.org/wiki/Event_Socket#api">http
 * ://wiki.freeswitch.org/wiki/Event_Socket#api</a>
 * </p>
 * 
 */
public final class ApiCommandImpl implements ApiCommand {

	private static final String SPACE = " ";

	private Command commandType;
	private List<Object> argsList = new ArrayList<Object>();
	private final StringBuilder cmdBuilder = new StringBuilder();
	private InboundClient inboundClient;
	private OutboundClient outboundClient;

	public ApiCommandImpl(InboundClient inboundClient) {
		this();
		Arguments.INSTANCE.validate(inboundClient).isNotNull();
		this.inboundClient = inboundClient;
	}

	ApiCommandImpl() {
		cmdBuilder.append(Command.API.getName().concat(SPACE));
	}

	public ApiCommandImpl(OutboundClient outboundClient) {
		this();
		Arguments.INSTANCE.validate(outboundClient).isNotNull();
		this.outboundClient = outboundClient;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.sendvox.freswitch.core.impl.ApiCommand#command(org.sendvox.freswitch
	 * .core.Command)
	 */
	@Override
	public ApiCommand command(Command command) {
		Arguments.INSTANCE.validate(command).isNotNull().isNotEmpty();
		this.commandType = command;
		cmdBuilder.append(command.getName().concat(SPACE));
		return this;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.sendvox.freswitch.core.impl.ApiCommand#arg(java.lang.Object)
	 */
	@Override
	public ApiCommand arg(Object... args) {
		Arguments.INSTANCE.validate(args).isNotNull().isNotEmpty();
		for (Object arg : args) {
			argsList.add(arg);
			cmdBuilder.append(arg.toString().concat(SPACE));
		}
		return this;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.sendvox.freswitch.core.impl.ApiCommand#getCommand()
	 */
	@Override
	public Command getCommand() {
		return commandType;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.sendvox.freswitch.core.impl.ApiCommand#getArgsList()
	 */
	@Override
	public List<Object> getArgsList() {
		return argsList;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.sendvox.freswitch.core.impl.ApiCommand#execute()
	 */
	@Override
	public ApiCommand execute() throws CommandException {
		if (inboundClient != null)
			inboundClient.execute(cmdBuilder.toString());
		else if (outboundClient != null)
			outboundClient.execute(cmdBuilder.toString());
		return this;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "ApiCommandImpl [command = " + cmdBuilder + "]";
	}

}
